package com.demo;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.Property;

import java.io.IOException;
import java.net.MalformedURLException;


public class PdfUtil {

    /**
     * 替换域中的图片
     */
    private static void replaceFieldImage(PdfDocument pdf, PdfFormField formField, String imgUrl) throws MalformedURLException {
        Rectangle rectangle = formField.getWidgets().get(0).getRectangle().toRectangle(); // 获取表单域的xy坐标
        PdfCanvas canvas = new PdfCanvas(pdf.getPage(1));
        ImageData image = ImageDataFactory.create(imgUrl);
        float imageWidth = image.getWidth();
        float imageHeight = image.getHeight();
        float rectangleWidth = rectangle.getWidth();
        float rectangleHeight = rectangle.getHeight();

        float tempWidth = 0;
        float tempHeight = 0;

        int result = 1; // 压缩宽度
        if (imageWidth > rectangleWidth) {
            tempHeight = imageHeight * rectangleWidth / imageWidth;
            if (tempHeight > rectangleHeight) {
                tempHeight = rectangleHeight;
                result = 2; // 压缩高度
            } else {
                tempWidth = rectangleWidth;
                tempHeight = imageHeight * rectangleWidth / imageWidth;
            }
        } else {
            if (imageHeight > rectangleHeight) {
                tempHeight = rectangleHeight;
                result = 2;
            } else {
                result = 3;
            }
        }

        float y = 0;

        if (result == 1) { // 压缩宽度
            y = rectangleHeight - tempHeight;
        } else if (result == 3) { // 不压缩
            y = rectangleHeight - imageHeight;
        }

        // y/=2; // 如果想要图片在表单域的上下对齐，这个值除以2就行。同理可以计算x的偏移

        if (result == 1) {
            canvas.addImage(image, rectangle.getX(), rectangle.getY() + y, tempWidth, false);
        } else if (result == 2) {
            canvas.addImage(image, rectangle.getX(), rectangle.getY(), tempHeight, false, false);
        } else if (result == 3) {
            canvas.addImage(image, rectangle.getX(), rectangle.getY() + y, false);
        }
    }

    public static void main(String[] args) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfReader("D:\\pdf_template\\template.pdf"), new PdfWriter("D:\\pdf_template\\1.pdf"));

        PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);

        PdfFormField formField1 = form.getField("logo");
        replaceFieldImage(pdf, formField1, "D:\\pdf_template\\logo.png");

        PdfFormField formField2 = form.getField("institution_name");
        formField2.setValue("比格威视光中心");

        PdfFormField formField3 = form.getField("student_name");
        formField3.setValue("猪八戒");
        PdfFormField formField4 = form.getField("sex");
        formField4.setValue("男");
        PdfFormField formField5 = form.getField("school_name");
        formField5.setValue("望子成龙小学");
        PdfFormField formField6 = form.getField("grade");
        formField6.setValue("一年级");
        PdfFormField formField7 = form.getField("class_name");
        formField7.setValue("1班");
        PdfFormField formField8 = form.getField("birthday");
        formField8.setValue("2020-01-02");
        PdfFormField formField9 = form.getField("student_qr_code");
        replaceFieldImage(pdf, formField9, "D:\\pdf_template\\student_qr_code.png");
        PdfFormField formField10 = form.getField("institution_qr_code");
        replaceFieldImage(pdf, formField10, "D:\\pdf_template\\student_qr_code.png");

        PdfFormField formField11 = form.getField("more");
        formField11.setValue("【比格威视光中心】");

        PdfFormField formField12 = form.getField("institution_name2");
        formField12.setValue("比格威视光中心");
        PdfFormField formField13 = form.getField("text");
        String s="请关注做信公众号，点击公众号中的菜单栏，扫描本卡中的“学生档察二维码”或输入学生本人身份信息查询视力检测结果，如显示视力正常，请保持良好的用眼习惯，预防近视发生。如显示存在视力问题，建议尽快到正规医院眼科就诊，做到早发现、早干预。为了学生的眼睛健康，请家长务必做好本次视防控监测工作。";
        formField13.setValue(s);
        PdfFormField formField14 = form.getField("address");
        formField14.setValue("联系地址：江苏省虎丘区望子成龙路28号");
        PdfFormField formField15 = form.getField("mobile");
        formField15.setValue("咨询电话:13484488498");

        form.flattenFields();// 锁定表单，不让修改
        pdf.close();
    }
}
